<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mincentive extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }    
    
    public function dbincentives($number = 0,$offset = 0)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT ic.id,ic.title,ic.image,ic.summary,ic.admin_id,ic.active,u.username FROM incentive_new AS ic LEFT JOIN user AS u ON u.id = ic.admin_id WHERE 1=1 ";
        
        $keyword = $this->getParamString('keyword');
        if(!in_array($keyword, array(null, ''))){
            $sql .= "AND ic.`title` LIKE '%".$keyword."%' ";
        }
        $sql .= "ORDER BY ic.id DESC";
        
        $count = $this->db->query($sql)->num_rows();
        if($offset >= $count && $count != 0){
            $this->load->view('template/404',$this->data);
            return false;
        }
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        return array('data' => $xs, 'count' => $count);
    }
    
    private function remove_null_tags($st){
		$Char 	= '<p> </p>|<p>&nbsp;</p>|<p></p>|<span> </span>|<span>&nbsp;</span>|<span></span>|<div> </div>|<div>&nbsp;</div>|<div></div>';
		$explChar 	= explode("|", $Char);
		return str_replace($explChar, '', $st);
	}
    public function dbaddincentives($title,$img,$summary,$content,$admin_id,$img1,$ecoupon)
    {
        $post = strip_tags($content,"<a><br>");
        $title_ascii = $this->get_ascii($title);
        $data = array(
            'title' => $title,
            'title_ascii' => $title_ascii,
            'image' => $img,
            'slide_image' => $img1,
            'summary' => $summary,
            'content' => $this->remove_null_tags($post),
            'admin_id' => $admin_id,
            'created' => date("Y-m-d H:i:s"),
			'active'	=> $this->getParamInt('active')
        );
        
        //echo '<pre>';
        //die(print_r($data));
        
        $this->db->insert('incentive_new', $data);
        return 200;
    }
    
    public function dbgetrestaurant()
    {
        $sql = "SELECT id,name FROM restaurant";
        $data = $this->db->query($sql)->result_array();
        return $data;
    }
    
    
    public function dbeditincentives($id,$title,$img,$summary,$content,$admin_id,$img1,$ecoupon)
    {
        $post = strip_tags($content,"<a><br>");
        $title_ascii = $this->get_ascii($title);
        $data = array(
            'title' => $title,
            'title_ascii' => $title_ascii,
            'summary' => $summary,
            'content' => $this->remove_null_tags($post),
            'admin_id' => $admin_id,
            'created' => date("Y-m-d H:i:s"),
			'active'	=> $this->getParamInt('active')
        );
        if(!in_array($img, array(null, '', '0')))
        {
            $data['image'] = $img;
        }
        if(!in_array($img1, array(null, '', '0')))
        {
            $data['slide_image'] = $img1;
        }
        
        //echo '<pre>';
        //die(print_r($data));
        
        $this->db->update('incentive_new', $data, "id = '".$id."'");
        return 200;
    }
    
    public function dbgetinfoincentives($id)
    {
        $data = $this->db->query('SELECT * FROM incentive_new WHERE id = "'.$id.'"')->row_array();
        return $data; 
    }    
    
    public function dbactiveincentives($id)
    {
        $de_active = array('active' => 0);
        $active = array('active' => 1);
        $data = $this->db->query("SELECT active FROM incentive_new WHERE id = '".$id."'")->row_array();
        if($data['active'] == 0)
        {
            $this->db->update('incentive_new', $active, "id = '".$id."'");
        }else{
            $this->db->update('incentive_new', $de_active, "id = '".$id."'");
        }
        return 200;
    }
    
    public function dbdeleteincentives($id)
    {
        $path = $this->db->query("SELECT image,slide_image FROM incentive_new WHERE id = '".$id."'")->row_array();
        //echo '<pre>';
        //die(print_r($path));
        
        foreach($path AS $key => $value)
        {
            @unlink(PROJECT_PATH.'/data/'.$value);
        }
        
        $this->db->delete('incentive_new', array('id' => $id));
        return 200;
    }
    
    public function selecteCoupon()
    {
        $sql = "SELECT id,title AS name FROM ecoupon";
        $data = $this->db->query($sql)->result_array();
        return $data;
    }
}
